package com.deer.wms.ware.task.strategy.waveDetail;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.model.SO.SoDetail;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.Wave.WaveDetail;
import com.deer.wms.ware.task.model.Wave.WaveMaster;
import com.deer.wms.ware.task.model.enums.WaveDetailEnum;
import com.deer.wms.ware.task.service.SoDetailService;
import com.deer.wms.ware.task.service.SoMasterService;
import com.deer.wms.ware.task.service.WaveDetailService;
import com.deer.wms.ware.task.service.WaveMasterService;
import com.deer.wms.ware.task.strategy.DetailStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
/**
 * @Author: xu
 * @Date: 2022/08/01/17:13
 * @Description:
 */
public class Processing implements DetailStrategy {

    @Autowired
    private SoDetailService soDetailService;

    @Autowired
    private WaveDetailService waveDetailService;

    @Autowired
    private SoMasterService soMasterService;
    @Autowired
    private CodeRuleService codeRuleService;


    @Autowired
    private WaveMasterService waveMasterService;
    //待分配  0
    @Override
    public Integer getState() {
     return    WaveDetailEnum.WAVE_STATE_PROCESSING.getStatenum();
    }

    @Override
    public void allocation(SoDetail oldSoDetail, SoDetail newDetail, String waveCode) {
        if (newDetail == null) {
            //如果老单子删除
            soDetailService.removeById(oldSoDetail.getSoDetailId());
            WaveDetail waveDetail = waveDetailService.getById(oldSoDetail.getWaveDetailId());
            //已分配
            if (waveDetail.getQuantity() > oldSoDetail.getOrderQuantity()) {
                waveDetail.setQuantity(waveDetail.getQuantity() - oldSoDetail.getOrderQuantity());
                waveDetailService.updateById(waveDetail);
            } else {
                waveDetailService.removeById(waveDetail);
            }
         } else if (ObjectUtil.isEmpty(oldSoDetail)) {
            //如果老单子为空则新增一条
            newDetail.setState(0);
            soDetailService.save(newDetail);
            SoMaster soMaster = soMasterService.getOneByField("bill_no", newDetail.getBillNo());
            WaveDetail waveDetail = waveDetailService.getOne(new QueryWrapper<WaveDetail>()
                    .eq("wave_code", waveCode)
                    .ne("state", 0)
                    .eq("item_code", newDetail.getItemCode())
                    .eq("rob", newDetail.getRob())
                    .last("limit 1"));
            if (ObjectUtil.isNull(waveDetail)) {
                WaveMaster waveMaster = waveMasterService.getOneByField("code", waveCode);
                waveDetail = new WaveDetail(newDetail);
                waveDetail.setWaveDetailCode(codeRuleService.generateCodeByRedis("waveDetailCode"));
                waveDetail.setRob(newDetail.getRob());
                waveDetail.setQuantity(newDetail.getOrderQuantity());
                waveDetail.setSoMasterId(soMaster.getSoMasterId());
                //波次单信息
                waveDetail.setWaveMasterId(waveMaster.getWaveId());
                waveDetail.setWaveCode(waveMaster.getCode());
                waveDetail.setWareId(waveMaster.getWareId());
                waveDetail.setWareName(waveMaster.getWareName());
                waveDetailService.save(waveDetail);
                newDetail.setWaveDetailId(waveDetail.getWaveDetailId());
            } else {
                waveDetail.setQuantity(waveDetail.getQuantity() + newDetail.getOrderQuantity());
                newDetail.setWaveDetailId(waveDetail.getWaveDetailId());
            }
            soDetailService.updateById(newDetail);
            waveDetailService.saveOrUpdate(waveDetail);
        } else {
            //都有，则判断数量，有变化则修改
            if (ObjectUtil.notEqual(oldSoDetail.getOrderQuantity(), newDetail.getOrderQuantity())) {
                Double newQty = newDetail.getOrderQuantity();
                Double oldQty = oldSoDetail.getOrderQuantity();
                //数量增加
                oldSoDetail.setOrderQuantity(newQty);
                soDetailService.updateById(oldSoDetail);
                WaveDetail waveDetail = waveDetailService.getById(oldSoDetail.getWaveDetailId());
                if (ObjectUtil.isEmpty(waveDetail)) {
                    WaveMaster waveMaster = waveMasterService.getOneByField("code", waveCode);
                    waveDetail = new WaveDetail(newDetail);
                    waveDetail.setWaveDetailCode(codeRuleService.generateCodeByRedis("waveDetailCode"));
                    waveDetail.setRob(newDetail.getRob());
                    waveDetail.setQuantity(newDetail.getOrderQuantity());
                    SoMaster soMaster = soMasterService.getOneByField("bill_no", newDetail.getBillNo());
                    waveDetail.setSoMasterId(soMaster.getSoMasterId());
                    //波次单信息
                    waveDetail.setWaveMasterId(waveMaster.getWaveId());
                    waveDetail.setWaveCode(waveMaster.getCode());
                    waveDetail.setWareId(waveMaster.getWareId());
                    waveDetail.setWareName(waveMaster.getWareName());
                    waveDetailService.save(waveDetail);
                }
                waveDetail.setQuantity(waveDetail.getQuantity() + (newDetail.getOrderQuantity() - oldQty));
                waveDetailService.updateById(waveDetail);
            }
        }


    }
}
